<?php

namespace Controller\Advertising;

class Add extends \Controller\Base {

    public function actionIndex() {
        $this->setMenuPath('Advertising', 'Add');
        $id = $this->getInt('id');
        if ($this->isPost()) {
            $this->save();
        }
        $advertising = \Data\Advertising::instance()->getAdvertisingById($id);
        $advertisingPosition = \Data\Advertising::instance()->getAdvertisingPosition();//广告位置
        if (empty($advertising)) {
            $advertising = array(
                'Id' => '',
                'Title' => '',
                'Content' => '',
                'Type' => '',
                'Status' => '',
                'Cover' => '',
                'Url' => '',
                'Jump' => ''
            );
        }
        $this->assign('advertising', $advertising);
        $this->assign('advertisingPosition', $advertisingPosition);
        $this->assign('id', $id);
        $this->display();
    }

    /**
     * 保存广告
     */
    private function save() {
        $data = array();
        $id = $this->postInt('id');
        $data['Title'] = $this->postParam('title');
        $data['Content'] = $this->postParam('content');
        $data['Cover'] = $this->postParam('cover');
        $data['Type'] = $this->postParam('type');
        $data['Jump'] = $this->postParam('jump');
        $data['Url'] = $this->postParam('url');
        if ($this->postInt('status')) {
            $data['Status'] = $this->postInt('status');
        } else {
            $data['Status'] = 0;
        }
        if ($id) {
            $data['UpdateTime'] =  time();
            \Data\Advertising::instance()->updateAdvertising($id, $data);
            $this->addSuccessMsg('修改成功！');
        } else {
            $data['CreateTime'] = $data['UpdateTime'] =  time();
            $id =  \Data\Advertising::instance()->addAdvertising($data);
            if ($id) {
                $this->addSuccessMsg('添加成功!');
            } else {
                $this->addWarningMsg('添加失败！');
            }
        }
    }
}